$(document).ready(function(){
    $("a.overview_edit").click(function(){
    	var overview_edit = $(this);
    	var container = overview_edit.prev("div.overview");
    	var id = container.attr("id");
    	var text = container.html();

    	function leaveEdit() {
    		$("a.cancel").click(function(){
    			container.html(text);
    			overview_edit.show();
    		});
    	}

        $.get(overview_edit.attr("href"), {field:id,}, function(data){
    		if (data.ok) {
    			container.html(data.form);
    			container.find("textarea").attr("name", id).focus();
    			overview_edit.hide();

    			leaveEdit();

    			function postData() {
    				container.find("input[type='submit']").click(function(){
    					$.post(overview_edit.attr("href"), container.find("form").serialize(), function(data){
    						if (data.ok) {
    							container.html(data.text);
    							overview_edit.show();
    						}else {
    							container.html(data.form);
    							container.find("textarea").attr("name", id).focus();
    							leaveEdit();
    							postData();
    						}
    					}, "json");
    				});
    			}
    			postData();
    		}
    	}, "json");
    });
});